package com.hfb.mashibing.alip8.designpattern._11flyweight;


import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.ToString;

public class Flyweight {
    public static void main(String[] args) {
        BulletPool bulletPool = new BulletPool();
        for (int i = 0; i < 1; i++) {
            System.out.println(bulletPool.getBullet());
        }
    }
}
@ToString
class Bullet {
    public UUID id = UUID.randomUUID();
    boolean living = true;

    public Bullet() {}
    public Bullet(boolean living) {
        this.living = living;
    }
}
class BulletPool {
    List<Bullet> bullets = new ArrayList<Bullet>(){{
        for (int i = 0; i < 100; i++) {
            if (i%30 == 0) add(new Bullet(false));
            add(new Bullet());
        }
    }};
    public Bullet getBullet(){
        return bullets.stream().filter(b -> !b.living).findFirst().get();
    }
}

